﻿#include "PhysicsDialogBeam.h"
#include "ui_PhysicsDialogBeam.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKPhyscisPropertyBeam.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include <QMessageBox>

namespace GUI
{
    PhysicsDialogBeam::PhysicsDialogBeam(PhysicsDlgExecType type, int propid/*=-1*/,QWidget* parent /*= nullptr*/)
        :AbstractMaterialDialog(type,parent)
    {
        _ui = new Ui::PhysicsDialogBeam;
        _ui->setupUi(this);
        //初始化材料下拉列表
        if (!_matMgr)return;
        QList<Acoustics::FITKAcousticsAbsMaterial*> list = _matMgr->getAcousticsMaterials(Acoustics::FITKAcousticsAbsMaterial::ISOSolid);
        if (!list.isEmpty()) {
            for (auto mate : list) {
                _ui->comboBox_material_2->addItem(mate->getDataObjectName(), mate->getDataObjectID());
            }
        }
        //获取材料对象
        if (_propMgr && propid > 0)
            _prop = dynamic_cast<Acoustics::FITKPhyscisPropertyBeam*>(_propMgr->getDataByID(propid));

        this->init();

    }
    PhysicsDialogBeam::~PhysicsDialogBeam()
    {
        if (_ui) delete _ui;
    }
    void PhysicsDialogBeam::on_pushButtonOK_clicked()
    {
        if (!this->checkInput()) return;
        //要处理的对象
        Acoustics::FITKPhyscisPropertyBeam* beam = dynamic_cast<Acoustics::FITKPhyscisPropertyBeam*>(_prop);
        //保存执行的脚本
        QStringList str;
        QString name = _ui->lineEdit_name_2->text();

        //创建与复制,创建新对象
        if (_execType == GUI::PhysicsDlgExecType::Create || _execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            str.append("p=PhyscisProp.Beam()");
            str.append(QString("p.createPhyscisProp('%1')").arg(name));

            beam = new Acoustics::FITKPhyscisPropertyBeam;
            _propMgr->appendDataObj(beam);
        }
        if (!beam)return;
        
        QString matName = _ui->comboBox_material_2->currentText();
        double area = _ui->lineEdit_area_2->text().toDouble();
        double perimeter = _ui->lineEdit_perimeter_2->text().toDouble();
        double dx = _ui->lineEdit_dx_2->text().toDouble();
        double dy = _ui->lineEdit_dy_2->text().toDouble();
        double ixx = _ui->lineEdit_Ixx_2->text().toDouble();
        double iyy = _ui->lineEdit_Iyy_2->text().toDouble();
        double jzz = _ui->lineEdit_Jzz_2->text().toDouble();
        double qzz = _ui->lineEdit_Qzz_2->text().toDouble();

        if (_execType == GUI::PhysicsDlgExecType::Edit)
        {
            str.append(QString("p=PhyscisProp.Beam.GetBeam('%1')").arg(beam->getDataObjectName()));
            str.append(QString("p.setName('%1')").arg(name));
        }
        str.append(QString("p.setMaterial('%1')").arg(matName));
        str.append(QString("p.setMoments(%1,%2,%3,%4)").arg(ixx).arg(iyy).arg(jzz).arg(qzz));
        str.append(QString("p.setCrossSection(%1,%2)").arg(area).arg(perimeter));
        str.append(QString("p.setShearCenterOffset(%1,%2)").arg(dx).arg(dy));

        beam->setDataObjectName(name);
        beam->setMaterialID(_ui->comboBox_material_2->currentData().toInt());
        beam->setCrossSectionArea(area);
        beam->setShearOffsetX(dx);
        beam->setShearOffsetY(dy);
        beam->setPerimeter(perimeter);
        beam->setSecondMomentX(ixx);
        beam->setSecondMomentY(iyy);
        beam->setPolarMoment(jzz);
        beam->setTorsionalConstant(qzz);
    
        this->saveScript(str);
        this->accept();
    }
    void PhysicsDialogBeam::on_pushButtonCancel_clicked()
    {
        this->reject();
    }

    void PhysicsDialogBeam::init()
    {
        if (_propMgr == nullptr|| _matMgr==nullptr) return;

        if (_execType == GUI::PhysicsDlgExecType::Create)
        {
            QString name = _propMgr->checkName("New Physics Property Beam");
            _ui->lineEdit_name_2->setText(name);
        }
        if (_prop == nullptr) return;
        Acoustics::FITKPhyscisPropertyBeam* beam = dynamic_cast<Acoustics::FITKPhyscisPropertyBeam*>(_prop);
        if (!beam) return;
        // 设置各个属性的值
        _ui->lineEdit_name_2->setText(beam->getDataObjectName());

        int id1 = beam->getMaterialID();
        int index1 = _ui->comboBox_material_2->findData(id1);
        _ui->comboBox_material_2->setCurrentIndex(index1);

        _ui->lineEdit_area_2->setText(QString::number(beam->getCrossSectionArea()));
        _ui->lineEdit_dx_2->setText(QString::number(beam->getShearOffsetX()));
        _ui->lineEdit_dy_2->setText(QString::number(beam->getShearOffsetY()));
        _ui->lineEdit_perimeter_2->setText(QString::number(beam->getPerimeter()));
        _ui->lineEdit_Ixx_2->setText(QString::number(beam->getSecondMomentX()));
        _ui->lineEdit_Iyy_2->setText(QString::number(beam->getSecondMomentY()));
        _ui->lineEdit_Jzz_2->setText(QString::number(beam->getPolarMoment()));
        _ui->lineEdit_Qzz_2->setText(QString::number(beam->getTorsionalConstant()));

        //复制,更新名称
        if (_execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            QString text = QString("Copy of %1").arg(beam->getDataObjectName());
            _ui->lineEdit_name_2->setText(text);
        }
    }

    bool PhysicsDialogBeam::checkInput()
    {
        //检查数据合法性
        QList<QLineEdit*> checkUi;
        checkUi << _ui->lineEdit_area_2 << _ui->lineEdit_dx_2 << _ui->lineEdit_dy_2
            << _ui->lineEdit_perimeter_2 << _ui->lineEdit_Ixx_2 << _ui->lineEdit_Iyy_2
            << _ui->lineEdit_Jzz_2 << _ui->lineEdit_Qzz_2;
        for (QLineEdit* e : checkUi)
        {
            if (e == nullptr) continue;
            QString text = e->text().trimmed();
            bool ok = false;
            text.toDouble(&ok);
            if (!ok)
            {
                QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a number."));
                return false;
            }
        }
        if (_propMgr == nullptr) return false;
        //检查名称
        QString text = _ui->lineEdit_name_2->text().trimmed();
        if (text != "") {
            QString newText = _propMgr->checkName(text);
            if (text == newText) return true;
            //与自己重名
            if (_propMgr->getDataByName(text) == _prop) return true;
        }
        QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a new Name."));
        return false;
    }

}
